import { _decorator, Animation, AnimationClip, AnimationManager, Component, EventKeyboard, Input, input, KeyCode, Layers, Node, RigidBody, TiledLayer, TiledMap, TiledTile, Vec2, Vec3 } from 'cc';
import { PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

@ccclass('GameController')
export class GameController extends Component {

    @property(Node)
    player: Node;

    @property(TiledMap)
    tileMap: TiledMap;

    private playerAnimationComponent: Animation;

    private playerMoveSpeed: number = 3;

    private playerController: PlayerController;

    private treeLayer: TiledLayer;

    protected onEnable(): void {

        this.playerController = this.player.getComponent(PlayerController);
        this.playerAnimationComponent = this.player.getComponent(Animation);
        // let tiledLayers = this.getComponentInChildren(TiledMap).getLayer("Tile Layer 3");

        // console.log('图层！')
        // console.log(tiledLayers)
        // var pos = tiledLayers.getPositionAt(0, 0);
        // console.log(pos)

        // this.mapInit("Tile Layer 2", 1, this.tileMap);
        this.treeLayer = this.tileMap.getLayer('tree');

        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this)
    }

    protected onDisable(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this)
    }

    protected onLoad(): void {

    }



    // 动态生成地图刚体 name: 需要生成刚体的层次名字， groupIndex: 分组index  tileMap: 地图
    // mapInit(name: string, groupIndex: number, tileMap: TiledMap) {
    //     let tileSize = tileMap.getTileSize();  // 地图小块尺寸 
    //     let layer: TiledLayer = tileMap.getLayer(name);  // 得到阻挡层
    //     let layerSize = layer.getLayerSize();  // 得到阻挡层尺寸

    //     for (let i = 0; i < layerSize.width; i++) {
    //         for (let j = 0; j < layerSize.height; j++) {
    //             let tiled: TiledTile = layer.getTiledTileAt(i, j, true);  // 拿到小块
    //             // 如果元素存在
    //             if (tiled.grid != 0) {
    //                 let body: RigidBody = tiled.node.addComponent(RigidBody);  // 动态添加RigidBody刚体
    //                 body.type = RigidBody.Type.STATIC;  // 设置为静态，防止下落
    //                 tiled.node.set = 'hinder'; // 将这个节点设置为wall
    //                 // tiled.node.groupIndex = groupIndex;  // 设置节点的groupIndex

    //                 // let collider = tiled.node.addComponent(cc.PhysicsBoxCollider); // 添加碰撞区域

    //                 // collider.offset = cc.v2(tileSize.width / 2, tileSize.height / 2)  // 设置偏移量, 除2是为了拿到中心点
    //                 // collider.size = tileSize; //包围盒的大小
    //                 // collider.apply();  //回调
    //                 console.log('小块存在！！！ 坐标：x:' + i + "y:" + j)

    //             }
    //         }
    //     }
    // }

    tryMoveToNewTile(newTile: Vec3) {

        var mapSize = this.tileMap.getMapSize();
        // if (newTile.x < 0 || newTile.x >= mapSize.width) return;
        // if (newTile.y < 0 || newTile.y >= mapSize.height) return;

        if (this.treeLayer.getTileGIDAt(newTile.x, newTile.y)) {//GID=0,则该Tile为空
            console.log('位置异常 This way is blocked!');
        } else {
            console.log('位置正常')
        }

    }


    onKeyDown(event: EventKeyboard) {

        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this.playerController.velocity.y = this.playerMoveSpeed
                this.playerAnimationComponent.play('player_move_w')
                console.log(this.playerAnimationComponent.clips)
                break;
            case KeyCode.KEY_A:
                this.playerController.velocity.x = -(this.playerMoveSpeed)
                this.playerAnimationComponent.play('player_move_a')
                break;
            case KeyCode.KEY_S:
                this.playerController.velocity.y = -(this.playerMoveSpeed)
                this.playerAnimationComponent.play('player_move_s')
                break;
            case KeyCode.KEY_D:
                this.playerController.velocity.x = this.playerMoveSpeed
                this.playerAnimationComponent.play('player_move_d')
                break;
            default:
                break;
        }

    }

    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
            case KeyCode.KEY_A:
            case KeyCode.KEY_S:
            case KeyCode.KEY_D:
                this.playerController.velocity.x = 0;
                this.playerController.velocity.y = 0;
                this.playerAnimationComponent.stop()
                console.log('调用')
                this.tryMoveToNewTile(this.player.position)
            default:
                break;
        }
    }

    start() {

    }

    update(deltaTime: number) {

    }
}


